package ${daotest.packageName};

<#if e.jspNeedDate>
import java.util.Date;
</#if>
import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.transaction.TransactionConfiguration;

import ${context.dao.fullName};
import ${context.bean.fullName};

/**
 * Generated by devtool.
 * 
 * @author ${author}
 *
 */
@ContextConfiguration(locations = "classpath:applicationContext.xml")
@TransactionConfiguration(transactionManager = "txManager", defaultRollback = false)
public class ${daotest.className}  extends AbstractTransactionalJUnit4SpringContextTests {
	private final static Logger log = LoggerFactory.getLogger(${daotest.className}.class);
	
    @Autowired
	private ${context.dao.className} ${context.dao.className?uncap_first};
	
    @Before
	public void setUp() throws Exception {

	}

	@After
	public void tearDown() throws Exception {
	
	}
	
	public ${context.bean.className} createBean(){
	  ${context.bean.className} t = new ${context.bean.className}();
	   <#list listx as x>
		<#if x.insertAble='1'>
			<#if x.javaType='String'>
		t.set${x.name?cap_first}("${x.dbName}");
		    </#if>
		    <#if x.javaType='Integer'>
		t.set${x.name?cap_first}(1);
		    </#if>
		    <#if x.javaType='Date'>
		t.set${x.name?cap_first}(new Date());
		    </#if>
		</#if>
		</#list>
		return t;
	}

	@Test
	public void testAdd() {
		${context.bean.className} t = createBean();
		${context.dao.className?uncap_first}.add(t);
		Object id = t.get${e.primaryColumn.name?cap_first}();
		log.debug("数据插入完成，数据主键是：{}",id);
		assertTrue("保存Bean", id != null);
	}
	
	
	@Test
	public void testGet() {
		${context.bean.className} t =  createBean();
		${context.dao.className?uncap_first}.add(t);
		Object id = t.get${e.primaryColumn.name?cap_first}();
		log.debug("数据插入完成，数据主键是：{}",id);
		assertTrue("保存Bean", id != null);
		
		${context.bean.className} s = ${context.dao.className?uncap_first}.get(id);
		assertTrue("查询Bean", s != null);
		<#list listx as x>
		<#if x.insertAble='1'>
		assertEquals("${x.label}", t.get${x.name?cap_first}(), s.get${x.name?cap_first}());
		</#if>
		</#list>
	}
	
	@Test
	public void testDelete() {
		${context.bean.className} t = createBean();
		${context.dao.className?uncap_first}.add(t);
		Object id = t.get${e.primaryColumn.name?cap_first}();
		log.debug("数据插入完成，数据主键是：{}",id);
		assertTrue("保存Bean", id != null);
		
		${context.bean.className} s = ${context.dao.className?uncap_first}.get(id);
		assertTrue("删除前查询Bean", s != null);
		
		${context.dao.className?uncap_first}.deleteById(id);
		s = ${context.dao.className?uncap_first}.get(id);
		assertTrue("删除后查询Bean", s == null);

	}
	
	@Test
	public void testUpdate() {
		${context.bean.className} t =  createBean();
		${context.dao.className?uncap_first}.add(t);
		Object id = t.get${e.primaryColumn.name?cap_first}();
		log.debug("数据插入完成，数据主键是：{}",id);
		assertTrue("保存Bean", id != null);
		
		${context.bean.className} s = ${context.dao.className?uncap_first}.get(id);
		assertTrue("查询Bean", s != null);
		<#list listx as x>
		<#if x.updateAble='1'>
		  	<#if x.javaType='String'>
		t.set${x.name?cap_first}("${x.dbName}");
		    </#if>
		    <#if x.javaType='Integer'>
		t.set${x.name?cap_first}(1);
		    </#if>
		    <#if x.javaType='Date'>
		t.set${x.name?cap_first}(new Date());
		    </#if>
		</#if>
		</#list>
		
		${context.dao.className?uncap_first}.update(t);
		s = ${context.dao.className?uncap_first}.get(id);
		assertTrue("查询Bean", s != null);
		<#list listx as x>
		<#if x.updateAble='1'>
		assertEquals("${x.label}", t.get${x.name?cap_first}(), s.get${x.name?cap_first}());
		</#if>
		</#list>
		
	}


}
